﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KM.ENT;

namespace KM.DAT
{
    public class datLoaiSanPham
    {
        public datLoaiSanPham()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        SqlCommand cmd;
        SqlDataReader dr;
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        #region  === t?o m?i và s?a LoaiSanPham===
        public void createEdit(entLoaiSanPham entLSP)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiSanPhamCreateEdit", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[5];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[1] = new SqlParameter("TenNhomSanPham", DbType.Int32);
            sqla[2] = new SqlParameter("IdCha", DbType.Int32);
            sqla[3] = new SqlParameter("ThuTu", DbType.Int32);
            sqla[4] = new SqlParameter("TrangThai", DbType.Int32);
            
            sqla[0].Value = entLSP.Id;
            sqla[1].Value = entLSP.TenNhomSanPham;
            sqla[2].Value = entLSP.IdCha;
            sqla[3].Value = entLSP.ThuTu;
            sqla[4].Value = entLSP.TrangThai;


            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion

        #region  === getall LoaiSanPham===
        public List<entLoaiSanPham> getAll()
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiSanPhamGetAll", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;

            dr = cmd.ExecuteReader();
            entLoaiSanPham entLSP = new entLoaiSanPham();
            List<entLoaiSanPham> lst = new List<entLoaiSanPham>();
            while (dr.Read())
            {
                entLSP = new entLoaiSanPham();
                entLSP.Id = Convert.ToInt32(dr["Id"].ToString());
                entLSP.TenNhomSanPham = dr["TenNhomSanPham"].ToString();
                entLSP.IdCha = Convert.ToInt32(dr["IdCha"].ToString());
                entLSP.ThuTu = Convert.ToInt32(dr["ThuTu"].ToString());
                entLSP.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());

                lst.Add(entLSP);
            }
            dr.Close();
            sqlConnection.Close();
            return lst;
        }
        #endregion

        #region  === getinfo LoaiSanPham===
        public entLoaiSanPham getInfo(int Id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiSanPhamGetInfo", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value =Id;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entLoaiSanPham entLSP = new entLoaiSanPham();
            if (dr.Read())
            {
                entLSP.Id = Convert.ToInt32(dr["Id"].ToString());
                entLSP.TenNhomSanPham = dr["TenNhomSanPham"].ToString();
                entLSP.IdCha = Convert.ToInt32(dr["IdCha"].ToString());
                entLSP.ThuTu = Convert.ToInt32(dr["ThuTu"].ToString());
                entLSP.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());

            }
            dr.Close();
            sqlConnection.Close();
            return entLSP;
        }
        #endregion

        #region === xoa LoaiSanPham===
        public void delete(int id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LoaiSanPhamDeleteById", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = id;

            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion
    }
}